<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Gio_Core_File
{
	public static function createDirs($root, $path) 
	{
		$root 	 = rtrim($root, DS);
		$subDirs = explode(DS, $path);
		if ($subDirs == null) {
			return;
		}
		$currDir = $root;
		foreach ($subDirs as $dir) {
			$currDir = $currDir . DS . $dir;
			if (!file_exists($currDir)) {
				mkdir($currDir);
			}
		}	 
	}
	
	/**
	 * Get sub-directori(es) of a directory, not including the directory which
	 * its name is one of ".", ".." or ".svn"
	 *  
	 * @param string $dir Path to the directory
	 * @return array
	 */
	public static function getSubDir($dir) 
	{
		if (!file_exists($dir)) {
			return array();
		}
		
		$subDirs 	 = array();
		$dirIterator = new DirectoryIterator($dir);
		foreach ($dirIterator as $dir) {
            if ($dir->isDot() || !$dir->isDir()) {
                continue;
            }
            $dir = $dir->getFilename();
            if ($dir == '.svn') {
            	continue;
            }
            $subDirs[] = $dir;
		}
		return $subDirs;
	}
	
	public static function writeToFile($file, $content)
	{
		$f = @fopen($file, 'w');
		@fwrite($f, $content);
		@fclose($f);
	}
	
/**
	 * @param string $dir
	 * @return boolean
	 */
	public static function deleteRescursiveDir($dir) 
	{
		if (is_dir($dir)) {	        	
            $dir 	 = (substr($dir, -1) != DS) ? $dir . DS : $dir;
            $openDir = opendir($dir);
            while ($file = readdir($openDir)) {
                if (!in_array($file, array(".", ".."))) {
                    if (!is_dir($dir . $file)) {
                        @unlink($dir . $file);
                    } else {
                        self::deleteRescursiveDir($dir . $file);
                    }
                }
            }
            closedir($openDir);
            @rmdir($dir);
        }
        
        return true;
	}
	
	/**
	 * @param string $source
	 * @param string $dest
	 * @return boolean
	 */
	public static function copyRescursiveDir($source, $dest) 
	{
		$openDir = opendir($source);
		if (!file_exists($dest)) {
	    	@mkdir($dest);
		}
		while ($file = readdir($openDir)) {
			if (!in_array($file, array(".", ".."))) {
	        	if (is_dir($source . DS . $file)) { 
	                self::copyRescursiveDir($source . DS . $file, $dest . DS . $file); 
	            } else { 
	                copy($source . DS . $file, $dest . DS . $file); 
				} 
	        }
	    }
	    closedir($openDir);
		
		return true;
	}
	
	/**
	 * Get size of a directory
	 * 
	 * @param string $dir
	 * @return int $size: Total size
	 */
	public static function getDirSize($dir)
	{
		$size = 0;
		if (is_dir($dir)) {	        	
            $dir 	 = (substr($dir, -1) != DS) ? $dir . DS : $dir;
            $openDir = opendir($dir);
            while ($file = readdir($openDir)) {
                if (!in_array($file, array(".", ".."))) {
                    if (!is_dir($dir . $file)) {
                        $size += filesize($dir . $file);
                    } else {
                        $size += self::getDirSize($dir . $file);
                    }
                }
            }
            closedir($openDir);
        }
        return $size;
	}
	
	/**
	 * Format a file size
	 * 
	 * @param int $size: byte
	 * @return string file size in B or KB/MB/GB/TB
	 */
	public static function formatFileSize($size)
	{
		$units = array('B', 'KB', 'MB', 'GB', 'TB');
	    for ($i = 0; $size >= 1024 && $i < 4; $i++) {
	    	$size /= 1024;	
	    }
	    return round($size, 2) . ' ' . $units[$i];
	}
}